package com.fk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fk.reggie.common.BaseContext;
import com.fk.reggie.common.R;
import com.fk.reggie.domain.ShoppingCart;
import com.fk.reggie.service.ShoppingCartService;
import com.fk.reggie.mapper.ShoppingCartMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author 凡凯
 * @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
 * @createDate 2023-03-18 20:43:12
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
        implements ShoppingCartService {

    @Override
    @Transactional
    public ShoppingCart saveShoppingCart(ShoppingCart shoppingCart) {
        //为当前购物车商品设置所属用户
        Long userId = BaseContext.getThreadLocal();
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //查询该用户购物车下是否已经存在该商品
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        if (shoppingCart.getDishId() != null) {
            //1.用户加入到购物车的是菜品   按照菜品id加用户名查询
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else if (shoppingCart.getSetmealId() != null) {
            //2.用户加入到购物车的是套餐  按照套餐id加用户名查询
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = this.getOne(lambdaQueryWrapper);
        if (one != null) {
            //该用户已经添加过   更新数量
            one.setNumber(one.getNumber() + 1);
            this.updateById(one);
        } else {
            //该用户没添加过   添加购物车商品
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
            one = shoppingCart;
        }
        return one;
    }

    @Override
    @Transactional
    public void subShoppingCart(ShoppingCart shoppingCart) {
        Long userId = BaseContext.getThreadLocal();
        UpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new UpdateWrapper<>();
        shoppingCartUpdateWrapper.eq("user_id", userId);
        if (shoppingCart.getSetmealId() != null) {
            shoppingCartUpdateWrapper.eq("setmeal_id", shoppingCart.getSetmealId());
            //查询当前用户当前套餐的数量
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                    .eq(ShoppingCart::getUserId, userId);
            ShoppingCart one = this.getOne(queryWrapper);
            Integer number = one.getNumber();

            if (number == 1) {
                LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
                        .eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
                this.remove(shoppingCartLambdaQueryWrapper);
                return;
            }

            shoppingCartUpdateWrapper.set("number", number - 1);
            this.update(shoppingCartUpdateWrapper);
        } else if (shoppingCart.getDishId() != null) {
            shoppingCartUpdateWrapper.eq("dish_id", shoppingCart.getDishId());
            //查询当前用户当前套餐的数量
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getUserId, userId)
                    .eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart one = this.getOne(queryWrapper);
            Integer number = one.getNumber();

            //当前购物车数量为1
            if (number == 1) {
                LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
                        .eq(ShoppingCart::getDishId, shoppingCart.getDishId());
                this.remove(shoppingCartLambdaQueryWrapper);
                return;
            }

            shoppingCartUpdateWrapper.set("number", number - 1);
            this.update(shoppingCartUpdateWrapper);
        }
    }
}




